package DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.FollowPlan;
import model.Incident;
import model.Person;
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.RecoverableDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
/**
 * 账户数据库实现
 * @author james
 *
 */
@Repository("userDaoe")
public class UserdaoImp implements Userdao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public static int input = 0;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Person> searchPerson() {
		String sql = "select personID,F_name,L_name,position,contact_phone,contact_email,Age,Sex from person";
		System.out.println(sql);
 		return jdbcTemplate.query(sql, userRowmapper);
		// TODO Auto-generated method stub
 	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Incident> searchIncident() {
		String sql1 = "select incidentID,incident_location,General_incident_description,captured_initials_of_reporting_individual from incident";
		System.out.println(sql1);
		return jdbcTemplate.query(sql1, incidentRowmapper);
		// TODO Auto-generated method stub
 	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FollowPlan> searchPlan() {
		String sql2 = "select plan_id,plan_type,plan_description from followupplan";
		System.out.println(sql2);
		return jdbcTemplate.query(sql2, planRowmapper);
		// TODO Auto-generated method stub
 	}
	
	


 	@SuppressWarnings("rawtypes")
	protected RowMapper userRowmapper = new RowMapper(){//这里使用 RowMapper 定义了一个用于绑定person域对象的内部映射类
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Person user = new Person();
			user.setId(rs.getInt("personID"));
 			user.setF_name(rs.getString("F_name"));
			user.setL_name(rs.getString("L_name"));
			user.setPhone(rs.getInt("contact_phone"));
			user.setEmail(rs.getString("contact_email"));
			user.setPosition(rs.getString("position"));
			user.setAge(rs.getInt("Age"));
			user.setSex(rs.getString("Sex"));
 			return user;
		}
	};
	
 	@SuppressWarnings("rawtypes")
	protected RowMapper incidentRowmapper = new RowMapper(){//这里使用 RowMapper 定义了一个用于绑定incident域对象的内部映射类
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Incident user = new Incident();
			user.setId(rs.getInt("incidentID"));
			user.setDes(rs.getString("General_incident_description"));
			user.setInit(rs.getString("captured_initials_of_reporting_individual"));
			user.setLocation(rs.getString("incident_location"));
  			return user;
		}
	};
	
 	@SuppressWarnings("rawtypes")
	protected RowMapper planRowmapper = new RowMapper(){//这里使用 RowMapper 定义了一个用于绑定plan域对象的内部映射类
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			FollowPlan user = new FollowPlan();
			user.setId(rs.getInt("plan_id"));
			user.setDes(rs.getString("plan_description"));
			user.setType(rs.getString("plan_type"));
   			return user;
		}
	};


	@Override
	public void insertIncident(String incident_location, String Gen_des,String intal) {
		// TODO Auto-generated method stub
		String sql = "insert into incident(incident_location,General_incident_description,captured_initials_of_reporting_individual)values('"
				+ incident_location +"','"+Gen_des+"','"+intal+"')";
		System.out.print(sql);
		jdbcTemplate.update(sql);
	}

	@Override
	public void insertPlan(String type, String des) {
		String sql = "insert into followupplan(plan_type,plan_description)values('" + type+"','"+des+"')";
		System.out.print(sql);
		jdbcTemplate.update(sql);
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void insert(String fname, String lname, String posititon, int phone,String email,int age, String sex) {
		String sql = "insert into person(F_name,L_name,position,contact_phone,contact_email,Age,Sex)values('" 
				+ fname +"','"+lname+"','"+posititon+"'," + phone + ",'"+ email+"'," +age+ ",'"+sex+"')";
		System.out.print(sql);
        //save
        jdbcTemplate.update(sql);
 	}

	@SuppressWarnings("unchecked")
	@Override
	public Person findPerson(int id) {
		// TODO Auto-generated method stub
		String sql = "select personID,F_name,L_name,position,contact_phone,contact_email,Age,Sex from person where personID="+id;
		System.out.println(sql);
 		return jdbcTemplate.queryForObject(sql, userRowmapper);
 	}

	@SuppressWarnings("unchecked")
	@Override
	public Incident findIncident(int id) {
		String sql1 = "select incidentID,incident_location,General_incident_description,captured_initials_of_reporting_individual from incident where incidentID="+id;
		System.out.println(sql1);
		return jdbcTemplate.queryForObject(sql1, incidentRowmapper);
	}

	@SuppressWarnings("unchecked")
	@Override
	public FollowPlan findPlan(int id) {
		String sql2 = "select plan_id,plan_type,plan_description from followupplan where plan_id="+id;
		System.out.println(sql2);
		return jdbcTemplate.queryForObject(sql2, planRowmapper);
	}

	@Override
	public void updatePerson(String type, int id, String info) {
		if(type.equals("contact_phone") || type.equals("Age")){
			String sql = "update person set " +type+"="+info+" where personID="+id;
			System.out.println(sql);
			jdbcTemplate.update(sql);
		}
		else{
			String sql = "update person set " +type+"='"+info+"' where personID="+id;
			System.out.println(sql);
			jdbcTemplate.update(sql);
		}
		// TODO Auto-generated method stub
	}

	@Override
	public void deletePerson(int id) {
		String sql = "delete from person where personID= "+id;
		jdbcTemplate.update(sql);
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateIncident(String type, int id, String info) {
		String sql = "update incident set " +type+"='"+info+"' where incidentID="+id;
		System.out.println(sql);
		jdbcTemplate.update(sql);
 	}

	@Override
	public void deleteIncident(int id) {
		String sql = "delete from incident where incidentID= "+id;
		jdbcTemplate.update(sql);
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updatePlan(String type, int id, String info) {
		String sql = "update followupplan set " +type+"='"+info+"' where plan_id="+id;
		System.out.println(sql);
		jdbcTemplate.update(sql);
		
	}

	@Override
	public void deletePlan(int id) {
		String sql = "delete from followupplan where plan_id= "+id;
		jdbcTemplate.update(sql);
		// TODO Auto-generated method stub
		
	}






	
}
